package com.umeox.watch.moto.dataservice.stub;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.apiclient.model.VoipTokenDTO;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.interfaces.IVideocall;
import com.umeox.watch.moto.dataservice.services.ApiService;

import net.vidageek.mirror.dsl.Mirror;

import java.util.Locale;

public class VideocallStub extends android.os.Binder implements IVideocall {

    public VideocallStub() {
        this.attachInterface(this, DESCRIPTOR);
    }

    public static void registerBinder() {
        try {
            new Mirror().on("android.os.ServiceManager").invoke().method("addService").withArgs(BuildConfig.videlcallBinderName, new VideocallStub());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder asBinder() {
        return this;
    }

    @Override
    public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws RemoteException {
        String descriptor = DESCRIPTOR;
        switch (code) {
            case INTERFACE_TRANSACTION: {
                reply.writeString(descriptor);
                return true;
            }
            case TRANSACTION_INVITE_JOIN_VIDEO: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                int result = inviteJoinVideo(friendId, channel);
                reply.writeNoException();
                reply.writeInt(result);
                return true;
            }
            case TRANSACTION_INVITE_JOIN_VOIP: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                int result = inviteJoinVoip(friendId, channel);
                reply.writeNoException();
                reply.writeInt(result);
                return true;
            }
            case TRANSACTION_INVITE_JOIN_PTT: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                int result = inviteJoinPTT(friendId, channel);
                reply.writeNoException();
                reply.writeInt(result);
                return true;
            }
            case TRANSACTION_NOTIFY_CANCEL_VIDEO: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                boolean result = notifyCancelVideo(friendId, channel);
                reply.writeNoException();
                reply.writeByte((byte) (result ? 1 : 0));
                return true;
            }
            case TRANSACTION_NOTIFY_CANCEL_VOIP: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                boolean result = notifyCancelVoip(friendId, channel);
                reply.writeNoException();
                reply.writeByte((byte) (result ? 1 : 0));
                return true;
            }
            case TRANSACTION_NOTIFY_CANCEL_PTT: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                boolean result = notifyCancelPTT(friendId, channel);
                reply.writeNoException();
                reply.writeByte((byte) (result ? 1 : 0));
                return true;
            }
            case TRANSACTION_UPLOAD_VIDEO_TIME: {
                data.enforceInterface(descriptor);
                String friendId = data.readString();
                String channel = data.readString();
                long startTime = data.readLong();
                long endTime = data.readLong();
                boolean result = uploadVideoTime(friendId, channel, startTime, endTime);
                reply.writeNoException();
                reply.writeByte((byte) (result ? 1 : 0));
                return true;
            }
            case TRANSACTION_GET_VIDEO_TOKEN: {
                data.enforceInterface(descriptor);
                String hid = data.readString();
                String tuid = data.readString();
                String channel = data.readString();
                VoipTokenDTO result = getVoipToken(hid, tuid, channel);
                reply.writeNoException();
                reply.writeParcelable(result, flags);
                return true;
            }
            default: {
                return super.onTransact(code, data, reply, flags);
            }
        }
    }

    @Override
    public VoipTokenDTO getVoipToken(String hid, String tuid, String channel) {
        return ApiService.getVoipToken(hid,tuid,channel);
    }

    @Override
    public int inviteJoinVideo(String friendId, String channel) {
        Logger.i(String.format("邀请{%s}加入频道{%s}", friendId, channel));
        return ApiService.inviteJoinVoipVideo(1,friendId,channel);
    }

    @Override
    public int inviteJoinVoip(String friendId, String channel) {
        Logger.i(String.format("邀请{%s}加入频道{%s}", friendId, channel));
        return ApiService.inviteJoinVoipVideo(2, friendId, channel);
    }

    @Override
    public int inviteJoinPTT(String friendId, String channel)  {
        Logger.i(String.format("邀请{%s}加入PTT频道{%s}", friendId, channel));
        return ApiService.inviteJoinPTT(friendId, channel);
    }

    @Override
    public boolean notifyCancelVideo(String friendId, String channel) {
        Logger.i(String.format("通知{%s}退出频道{%s}", friendId, channel));
        return ApiService.notifyCancelVoipVideo(1,friendId, channel);
    }

    @Override
    public boolean notifyCancelVoip(String friendId, String channel) {
        Logger.i(String.format("通知{%s}退出频道{%s}", friendId, channel));
        return ApiService.notifyCancelVoipVideo(2, friendId, channel);
    }

    @Override
    public boolean notifyCancelPTT(String friendId, String channel)  {
        Logger.i(String.format("通知{%s}退出PTT频道{%s}", friendId, channel));
        return ApiService.notifyCancelPTT(friendId, channel);
    }

    @Override
    public boolean uploadVideoTime(String friendId, String channel, long startTime, long endTime) {
        Logger.i(String.format(Locale.CHINA,"上报与{%s}视频通话时长{%.2fs}", friendId, (float) (endTime - startTime) / 1000));
        return ApiService.uploadVideoTime(BaseApp.getInstance().deviceId, friendId, channel, startTime, endTime);
    }

}
